package com.penghaisoft.microwms.materialhand.web.outwarehouse.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.penghaisoft.microwms.common.data.model.RawRequisition;
import com.penghaisoft.microwms.common.data.model.RawRequisitionSn;
import com.penghaisoft.microwms.common.dto.Resp;
import com.penghaisoft.microwms.common.service.BaseService;
import com.penghaisoft.microwms.common.wms.model.RawStorageSn;
import com.penghaisoft.microwms.materialhand.web.outwarehouse.dao.RawRequisitionMapper;
import com.penghaisoft.microwms.materialhand.web.outwarehouse.dao.RawRequisitionSnMapper;
import com.penghaisoft.microwms.materialhand.web.outwarehouse.service.IMaterialRequisitionService;
import com.penghaisoft.microwms.materialhand.web.storage.dao.RawStorageSnMapper;

/**
 * @ClassName: MaterialRequisitionServiceImpl
 * @Description: 原材料手持报废出库服务实现
 * @author jzh
 * @date 2018年6月21日 上午10:29:15
 */
@Service
public class MaterialRequisitionServiceImpl extends BaseService implements IMaterialRequisitionService {

	@Autowired
	private RawRequisitionMapper rawRequisitionMapper;
	@Autowired
	private RawStorageSnMapper rawStorageSnMapper;
	@Autowired
	private RawRequisitionSnMapper rawRequisitionSnMapper;
	
	
	/*
	 * @Title: queryRawRequisition
	 * @Description: 返修数据查询
	 * @author jzh
	 * @date 2018年6月21日 上午10:30:19
	 */
	@Override
	public List<RawRequisition> queryRawRequisition(RawRequisition rawRequisition) {

		return rawRequisitionMapper.queryByAny(rawRequisition);
	}

	/*
	 * @Title: queryRequisitionDetail
	 * @Description: 需求数量大于实际数量，status不等于2
	 * @author jzh
	 * @date 2018年6月21日 下午2:27:04
	 */
	@Override
	public List<RawRequisition> queryRequisitionDetail(RawRequisition rawRequisition) {

		return rawRequisitionMapper.queryDetailNot2(rawRequisition);
	}

	/*
	 * @Title: queryRawStorageSn
	 * @Description: 根据条码查库存
	 * @author jzh
	 * @date 2018年6月21日 下午3:43:33
	 */
	@Override
	public List<RawStorageSn> queryRawStorageSn(RawStorageSn rawStorageSn) {

		return rawStorageSnMapper.queryByAny(rawStorageSn);
	}

	/* @Title: updateScanRequisition 
	 * @Description: 更改任务已分配数量为新数量，发货中 并且 新增记录,更改条码状态为已出库（保证事物）
	 * @author jzh
	 * @date 2018年6月22日 上午9:49:05 
	 */
	@Transactional
    @Override
	public Resp updateScanRequisition(RawRequisition tmpRequisition, RawRequisitionSn rawRequisitionSn,
			RawStorageSn newRawStorageSn) {
		
		//更改任务已分配数量为新数量，发货中
		Integer updateCnt = rawRequisitionMapper.updateBySelect(tmpRequisition);
		if (updateCnt.intValue() <= 0) {
			throw new RuntimeException("更改数量失败");
		}
		//新增记录rawRequisitionSn实体
		Integer createCnt = rawRequisitionSnMapper.create(rawRequisitionSn);
		if (createCnt.intValue() <= 0) {
			throw new RuntimeException("新增记录失败");
		}
		//更改条码状态为已出库
		Integer updateStorageSnCnt = rawStorageSnMapper.updateBySelect(newRawStorageSn);
		if (updateStorageSnCnt.intValue() <= 0) {
			throw new RuntimeException("更改条码状态为已出库失败");
		}
		return success();
	}

	@Override
	public Resp updateRequisition(RawRequisition rawRequisition) {
		Integer updateCnt = rawRequisitionMapper.updateByRequisitionNo(rawRequisition);
		System.out.print("更改数量："+updateCnt);
		if(updateCnt.intValue() >0){
			return success();
		}else{
			return fail("更改状态失败");
		}
	}
	
	/** 
	 * @Title queryMesNormalList
	 * @Description 查询返回给MES的【普通】的领料数据
	 * @author luoteng
	 * @date 2018年6月26日:上午10:44:35
	 * @param rawRequisitionSn
	 * @return List<RawRequisitionSn>
	 */
	@Override
	public List<RawRequisitionSn> queryMesNormalList(RawRequisitionSn rawRequisitionSn){
		return rawRequisitionSnMapper.queryMesNormalList(rawRequisitionSn);
	}
	
	/** 
	 * @Title queryMesRepairList
	 * @Description 查询返回给MES的【返修】的领料数据
	 * @author luoteng
	 * @date 2018年6月26日:下午2:31:58
	 * @param rawRequisitionSn
	 * @return List<RawRequisitionSn>
	 */
	@Override
	public List<RawRequisitionSn> queryMesRepairList(RawRequisitionSn rawRequisitionSn){
		return rawRequisitionSnMapper.queryMesRepairList(rawRequisitionSn);
	}

	@Override
	public List<RawRequisition> queryRequisitionNot2(RawRequisition rawRequisition) {
		return rawRequisitionMapper.queryRequisitionNot2(rawRequisition);
	}
	
	/** 
	 * @Title updateMesStatus
	 * @Description 更新 是否上传mes的状态
	 * @author luoteng
	 * @date 2018年6月26日:下午2:31:58
	 * @param rawRequisitionSn
	 * @return List<RawRequisitionSn>
	 */
	@Override
	@Transactional
	public Resp updateMesStatus(List<RawRequisitionSn> list, String loginName){
		Date now = new Date();
		if(list != null && !list.isEmpty()){
			for(RawRequisitionSn tmp : list){
//	            0未传mes;1已传mes
				tmp.setMesStatus("1");
				tmp.setLastModifiedBy(loginName);
				tmp.setGmtModified(now);
				rawRequisitionSnMapper.updateBySelect(tmp);
			}
		}
		
		return success();
	}

}
